﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using HumanManagement.BusinessObjects;

namespace HumanManagement.DataAccess
{
	public class PersonalProfileDA
	{

		#region ***** Init Methods ***** 
		public PersonalProfileDA()
		{
		}
		#endregion

		#region ***** Get Methods ***** 
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public PersonalProfile Populate(IDataReader myReader)
		{
			PersonalProfile obj = new PersonalProfile();
			obj.ID = Convert.ToInt32( myReader["ID"]);
            obj.BranchID = myReader["BranchID"] is DBNull ? 0 : Convert.ToInt32(myReader["BranchID"]);
			obj.FullName = (string) myReader["FullName"];
            obj.DateOfBirth = (DateTime)myReader["DateOfBirth"];
            obj.Sex = (string)myReader["Sex"];
            obj.FamilyRegisterAddress = (string)myReader["FamilyRegisterAddress"];
            obj.ContactAddress = (string)myReader["ContactAddress"];
            obj.NationalityID = myReader["NationalityID"] is DBNull ? 0 : Convert.ToInt32(myReader["NationalityID"]);
            obj.ReligionID = myReader["ReligionID"] is DBNull ? 0 : Convert.ToInt32(myReader["ReligionID"]);
            obj.NativeCountry = myReader["NativeCountry"] is DBNull ? "" : Convert.ToString(myReader["NativeCountry"]);
            obj.IdentificationNumber = myReader["IdentificationNumber"] is DBNull ? "" : Convert.ToString(myReader["IdentificationNumber"]);
            obj.SocialInsuranceNumber = myReader["SocialInsuranceNumber"] is DBNull ? "" : Convert.ToString(myReader["SocialInsuranceNumber"]);
            obj.LastDate = myReader["LastDate"] is DBNull ? DateTime.MinValue : (DateTime)myReader["LastDate"];
            obj.LastMakerID = myReader["LastMakerID"] is DBNull ? 0 : Convert.ToInt32(myReader["LastMakerID"]);
            obj.LastApproverID = myReader["LastApproverID"] is DBNull ? 0 : Convert.ToInt32(myReader["LastApproverID"]);
            obj.ProfileStatus = myReader["ProfileStatus"] is DBNull ? "" : Convert.ToString(myReader["ProfileStatus"]);
            obj.Description = myReader["Description"] is DBNull ? "" : Convert.ToString(myReader["Description"]);
            obj.Email = myReader["Email"] is DBNull ? "" : Convert.ToString(myReader["Email"]);
            obj.Picture = myReader["Picture"] is DBNull ? null : (byte[])myReader["Picture"];
			return obj;
		}

		/// <summary>
		/// Get PersonalProfile by id
		/// </summary>
		/// <param name="id">ID</param>
		/// <returns>PersonalProfile</returns>
		public PersonalProfile GetByID(int id, int PersonalProfileID)
		{
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_PersonalProfile_GetByID"
                                                                , Data.CreateParameter("ID", id)
                                                                , Data.CreateParameter("PersonalProfileID", PersonalProfileID)))
			{
				if (reader.Read())
				{
					return Populate(reader);
				}
				return null;
			}
		}

		/// <summary>
		/// Get all of PersonalProfile
		/// </summary>
		/// <returns>List<<PersonalProfile>></returns>
		public List<PersonalProfile> GetList()
		{
			using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_PersonalProfile_Get"))
			{
				List<PersonalProfile> list = new List<PersonalProfile>();
				while (reader.Read())
				{
				list.Add(Populate(reader));
				}
				return list;
			}
		}

		/// <summary>
		/// Get DataSet of PersonalProfile
		/// </summary>
		/// <returns>DataSet</returns>
		public DataSet GetDataSetOld()
		{
            return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure, "sproc_PersonalProfile_Get");
		}

        public DataSet GetDataSet(int PersonalProfileID)
        {
            return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure, "sproc_PersonalProfile_Get"
                                            , Data.CreateParameter("PersonalProfileID", PersonalProfileID));
        }
		/// <summary>
		/// Get all of PersonalProfile paged
		/// </summary>
		/// <param name="recperpage">record per page</param>
		/// <param name="pageindex">page index</param>
		/// <returns>List<<PersonalProfile>></returns>
        public List<PersonalProfile> GetListPaged(int recperpage, int pageindex)
		{
			using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_PersonalProfile_GetPaged"
							,Data.CreateParameter("recperpage", recperpage)
							,Data.CreateParameter("pageindex", pageindex)))
			{
				List<PersonalProfile> list = new List<PersonalProfile>();
				while (reader.Read())
				{
				list.Add(Populate(reader));
				}
				return list;
			}
		}

		/// <summary>
		/// Get DataSet of PersonalProfile paged
		/// </summary>
		/// <param name="recperpage">record per page</param>
		/// <param name="pageindex">page index</param>
		/// <returns>DataSet</returns>
		public DataSet GetDataSetPaged(int recperpage, int pageindex)
		{
			return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure,"sproc_PersonalProfile_GetPaged"
							,Data.CreateParameter("recperpage", recperpage)
							,Data.CreateParameter("pageindex", pageindex));
		}





		#endregion

		#region ***** Add Update Delete Methods ***** 
		/// <summary>
		/// Add a new PersonalProfile within PersonalProfile database table
		/// </summary>
		/// <param name="obj">PersonalProfile</param>
		/// <returns>key of table</returns>
		public int Add(PersonalProfile obj)
		{
			DbParameter parameterItemID = Data.CreateParameter("ID", obj.ID);
			parameterItemID.Direction = ParameterDirection.Output;
			SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure,"sproc_PersonalProfile_Add"
							,parameterItemID
							,Data.CreateParameter("BranchID", obj.BranchID)
							,Data.CreateParameter("FullName", obj.FullName)
							,Data.CreateParameter("DateOfBirth", obj.DateOfBirth)
							,Data.CreateParameter("Sex", obj.Sex)
							,Data.CreateParameter("FamilyRegisterAddress", obj.FamilyRegisterAddress)
							,Data.CreateParameter("ContactAddress", obj.ContactAddress)
							,Data.CreateParameter("NationalityID", obj.NationalityID)
							,Data.CreateParameter("ReligionID", obj.ReligionID)
							,Data.CreateParameter("NativeCountry", obj.NativeCountry)
							,Data.CreateParameter("IdentificationNumber", obj.IdentificationNumber)
							,Data.CreateParameter("SocialInsuranceNumber", obj.SocialInsuranceNumber)
							,Data.CreateParameter("LastDate", obj.LastDate)
							,Data.CreateParameter("LastMakerID", obj.LastMakerID)
							,Data.CreateParameter("LastApproverID", obj.LastApproverID)
							,Data.CreateParameter("ProfileStatus", obj.ProfileStatus)
							,Data.CreateParameter("Description", obj.Description)
                            ,Data.CreateParameter("Email", obj.Email)
                            ,Data.CreateParameter("Picture", obj.Picture)
			);
			return (int)parameterItemID.Value;
		}

		/// <summary>
		/// updates the specified PersonalProfile
		/// </summary>
		/// <param name="obj">PersonalProfile</param>
		/// <returns></returns>
		public void Update(PersonalProfile obj)
		{
			SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure,"sproc_PersonalProfile_Update"
							,Data.CreateParameter("ID", obj.ID)
							,Data.CreateParameter("BranchID", obj.BranchID)
							,Data.CreateParameter("FullName", obj.FullName)
							,Data.CreateParameter("DateOfBirth", obj.DateOfBirth)
							,Data.CreateParameter("Sex", obj.Sex)
							,Data.CreateParameter("FamilyRegisterAddress", obj.FamilyRegisterAddress)
							,Data.CreateParameter("ContactAddress", obj.ContactAddress)
							,Data.CreateParameter("NationalityID", obj.NationalityID)
							,Data.CreateParameter("ReligionID", obj.ReligionID)
							,Data.CreateParameter("NativeCountry", obj.NativeCountry)
							,Data.CreateParameter("IdentificationNumber", obj.IdentificationNumber)
							,Data.CreateParameter("SocialInsuranceNumber", obj.SocialInsuranceNumber)
							,Data.CreateParameter("LastDate", obj.LastDate)
							,Data.CreateParameter("LastMakerID", obj.LastMakerID)
							,Data.CreateParameter("LastApproverID", obj.LastApproverID)
							,Data.CreateParameter("ProfileStatus", obj.ProfileStatus)
							,Data.CreateParameter("Description", obj.Description)
                            ,Data.CreateParameter("Email", obj.Email)
                            ,Data.CreateParameter("Picture", obj.Picture)
			);
		}

		/// <summary>
		/// Delete the specified PersonalProfile
		/// </summary>
		/// <param name="id">ID</param>
		/// <returns></returns>
		public void Delete(int id)
		{
			SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure,"sproc_PersonalProfile_Delete", Data.CreateParameter("ID", id));
		}
		#endregion
	}
}
